function LocalStorageClient() {
	//添加键值对
	this.put = function(key, value) {
		// localStorage.setItem(key, value);
		uni.setStorage({
			key: key,
			data: value,
			success: function() {
				console.log('success');
			}
		});
	};

	//添加json对象
	this.putJSON = function(key, json) {
		let value = JSON.stringify(json);
		uni.setStorage({
			key: key,
			data: value,
			success: function() {
				console.log('success');
			}
		});
	};

	//根据key查找JSON对象
	this.getJSON = function(key) {
		console.log('qsqsqsqs');
		return uni.getStorage({
			key: key,
			success: function(res) {
				console.log(res.data);
				if (res.data == 'undefined') {
					return null;
				} else {
					return res.data
				}

			},
			fail: function(err) {
				console.log(err);
			}
		});
		// if (localStorage.getItem(key) == 'undefined')
		// // 	return null;
		// return JSON.parse(localStorage.getItem(key));
	};

	//根据key查找value
	this.get = function(key) {
		// if (localStorage.getItem(key) == 'undefined')
		// 	return null;
		// return localStorage.getItem(key)
		return uni.getStorage({
			key: key,
			success: function(res) {
				// console.log(res.data);
				if (res.data == 'undefined') {
					return null;
				} else {
					return res.data
				}

			}
		});
	};

	//移除key对应的元素
	this.remove = function(key) {
		uni.removeStorage({
			key: key,
			success: function(res) {
				console.log('success');
			}
		});
	};

	//清空localStorage
	this.clear = function() {
		uni.clearStorage();
	};

	//根据索引获得item，结果为一个json对象 {key,value}
	this.indexOf = function(index) {
		var item = {};
		if (localStorage.length <= index) {
			return null;
		}

		var key = uni.getStorageInfoSync().keys[i];
		var value = uni.getStorage({
			key: key,
			success: function(res) {
				return res.data
			}
		});
		item.key = key;
		item.value = value;
		return item;
	};

	/**
	 * localStorage缓存数据方法
	 * key: localStorage存储的key值，不以“cache_”开头的key，一律自动加上"cache_"前缀
	 * data: 存储的数据
	 * flag: 是否和用户相关 true or false
	 */
	this.save = function(key, data, flag) {
		if (!key || !data) {
			return false;
		}
		key = this._buildCacheKey(key);
		var d = {
			data: data,
			timestamp: Date.now()
		};
		if (flag) {
			var user = this._getUser();
			d.user = user;
		}
		try {
			this.putJSON(key, d);
		} catch (e) {
			// 异常，超出容量限制，清空缓存
			this.clearCache();
			return false;
		}
		return true;
	};

	/**
	 * localStorage缓存数据方法
	 * preKey: 存储的key值前缀，不以“cache_”开头的key，一律自动加上"cache_"前缀
	 * variableKey： key值可变部分，如id
	 * count： 相同前缀key下最多可以存储多少条
	 * data: 存储的数据
	 * flag: 是否和用户相关 true or false
	 */
	this.saveAssignCount = function(preKey, variableKey, count, data, flag) {
		if (!preKey || !data || !App.cacheEnable) {
			return false;
		}
		preKey = this._buildCacheKey(preKey);
		var d = {
			data: data,
			timestamp: Date.now()
		};
		if (flag) {
			var user = this._getUser();
			d.user = user;
		}
		if (this.getJSON(preKey + variableKey) == null) {
			// 不是同键值，需要检测是否需要删除老数据
			var cacheData = new Array();
			for (var i = 0; i < uni.getStorageInfoSync().keys.length; i++) {
				var storageKey = uni.getStorageInfoSync().keys[i];
				if (storageKey.indexOf(preKey) == 0) {
					// 取相同前缀key的缓存对象的key和时间，以便数量超过时删除
					cacheData.push({
						key: storageKey,
						time: this.getJSON(storageKey).timestamp
					});
				}
			}
			if (cacheData.length > count) {
				// 按时间从小到达排列
				cacheData.sort(function(a, b) {
					return (b.time - a.time);
				})
				for (var j = cacheData.length - 1; j >= count; j--) {
					this.remove(cacheData[j].key);
				}
			}
		}
		try {
			this.putJSON(preKey + variableKey, d);
		} catch (e) {
			// 异常，超出容量限制，清空缓存
			this.clearCache();
			return false;
		}
		return true;
	};

	/**
	 * 从localStorage读取缓存数据方法
	 * key: localStorage存储的key值
	 * overtime: 超时时间（分钟）,默认60分钟
	 */
	this.load = function(key, overtime) {
		// if (!key || !App.cacheEnable) {
		//     return null;
		// }
		key = this._buildCacheKey(key);
		var d = this.getJSON(key);
		if (d == null) {
			return null;
		}
		if (isNaN(overtime) || overtime < 0) {
			overtime = 60;
		}
		var user = this._getUser();
		if (d.timestamp + overtime * 60 * 1000 > Date.now()) {
			// 时间在有效期内
			if (!d.user || d.user === user) {
				// 存储的数据和用户无关或者同一用户
				return d.data;
			} else {
				// 不同用户，清除缓存
				this.remove(key);
				return null;
			}
		} else {
			// 超过有效期，清除缓存
			this.remove(key);
			return null;
		}
	};

	// 读写缓存信息时的用户(这里取商户的cookie) 这个项目用户信息放在key里面
	this._getUser = function() {
		if (typeof(loginUser) != 'undefined' && loginUser) {
			if (loginUser.mobile) {
				return loginUser.mobile;
			}
		}
		return '';
	};

	// 不以“cache_”开头的key，一律自动加上"cache_"前缀
	this._buildCacheKey = function(key) {
		if (key.indexOf("cache_") !== 0) {
			return "cache_" + key;
		}
		return key;
	};

	/**
	 * 删除缓存数据"cache_"开头的
	 */
	this.clearCache = function() {
		for (var i = 0; i < uni.getStorageInfoSync().keys.length; i++) {
			var storageKey = uni.getStorageInfoSync().keys[i];
			if (storageKey.indexOf("cache_") === 0) {
				localStorage.removeItem(storageKey);
			}
		}
	};


	this.takeOutParamObject = function() {
		var rt = uni.getStorageSync('paramObj');;
		if (!rt) {
			return {};
		}
		return JSON.parse(rt);
	};
	/**
	 * 注入页面之间跳转需要传递的参数
	 * @paramObj 一个JSON对象，用来传递参数，其格式如下:
	 *             {"articleId":"123456"}
	 */
	this.injectParamObject = function(paramObj) {
		paramObj = paramObj || {};
		uni.setStorageSync('paramObj', JSON.stringify(paramObj));
	};
}
var localStorageClient = new LocalStorageClient();
export default localStorageClient;